﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Store.Manager.ERP.Api.Helper;
using Store.Manager.ERP.Application.Dtos;
using Store.Manager.ERP.Application.Services;

namespace StoreManager.ERP.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        [HttpPost("login")]
        public async Task<IActionResult> Login([FromBody] UserDto userDto)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var result = await _userService.LoginAsync(userDto.Username, userDto.Password);
            if (isLoggedIn)
            {
                JwtHelper.IssueJwt(new JwtToken() { Uid = })
                return Ok("登录成功");
            }
            return Unauthorized("用户名或密码错误");
        }

        [HttpPost("register")]
        public async Task<IActionResult> Register([FromBody] UserDto userDto)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            await _userService.RegisterAsync(userDto);
            return CreatedAtAction(nameof(Login), new { username = userDto.Username }, "注册成功");
        }
    }
}
